/**
 * 
 */
package com.gordon.tips.dao;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.gordon.tips.domain.Reply;
import com.gordon.tips.util.Constants;

/**
 *****************************************************************************
 ** Module : Reply data access obect
 ** 
 ** Date:  Aug 16th,2011
 ** 
 ** Author: Gordon
 ** 
 ** Email: 462991284@qq.com
 ** 
 ** (C) Copyright 2011 Gordon Offie
 *****************************************************************************
 */

public class ReplyDao extends SqlMapClientDaoSupport {
	
	/**
	 * Get replies by topic's id
	 * @param id
	 * @return
	 * @Author: Gordon
	 */
	public List<Reply>getRepliesByTopicId(int id,int start)
	{
		Map<String ,Integer>map=new HashMap<String,Integer>();
		map.put("start", start);
		map.put("pageSize", 10);
		map.put("id", id);
		return getSqlMapClientTemplate().queryForList("getRepliesByTopicId",map);
	}
	
	/**
	 * Add a new reply
	 * @param reply
	 * @param topicId
	 * @param authorId
	 * @Author: Gordon
	 */
	public int addNewReply(Reply reply,int topicId,int authorId)
	{
		Map<String ,Object>map=new HashMap<String,Object>();
		map.put("content",reply.getContent());
		map.put("createTime",reply.createTime);
		map.put("floor",reply.getFloor());
		map.put("topicId",topicId);
		map.put("authorId",authorId);		
		
		Map<String,Integer>map2=new HashMap<String,Integer>();
		map2.put("score",Constants.SCORE_ADD_NEWREPLY);
		map2.put("id",authorId);
		
		Map<String,Object>map3=new HashMap<String,Object>();
		map3.put("lastReplyTime", new Date());
		map3.put("lastReplyerId", authorId);
		map3.put("id", topicId);
		
		
		getSqlMapClientTemplate().update("updateTopicReply",map3);
		getSqlMapClientTemplate().update("updateUserPost",authorId);
		getSqlMapClientTemplate().update("updateUserScore",map2);
		return (Integer) getSqlMapClientTemplate().insert("addNewReply",map);
	}
	
	/**
	 * edit a reply
	 * @param replyid
	 * @param content
	 * @Author: Gordon
	 */
	public void editReply(int replyId,String content)
	{
		Map<String,Object>map=new HashMap<String,Object>();
		map.put("id",replyId);
		map.put("content",content);
		getSqlMapClientTemplate().update("editReply",map);
	}
	
	/**
	 * Get the reply
	 * @param id
	 * @return
	 * @Author: Gordon
	 */
	public Reply getReplyById(int id)
	{
		return (Reply) getSqlMapClientTemplate().queryForObject("getReplyById",id);
	}
	
	/**
	 * Delete the reply
	 * @param reply The reply to be deleted.
	 * @Author: Gordon
	 */
	public void delRely(Reply reply)
	{
		getSqlMapClientTemplate().delete("delReply",reply.getId());
	}

}
